<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/icon_logo.png" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- 添加Content Security Policy防止第三方脚本干扰 -->
    <meta
      http-equiv="Content-Security-Policy"
      content="script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'none';"
    />
    <title>京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物！</title>
    <link rel="stylesheet" href="//at.alicdn.com/t/c/font_5000323_mzvot7gnou.css" />

    <!-- 内联错误拦截脚本 - 在页面加载时立即执行 -->
    <script>
      ;(function () {
        'use strict'

        console.log('🚀 Early error interceptor starting...')

        // 立即拦截 unhandledrejection 事件
        window.addEventListener('unhandledrejection', function (event) {
          const reason = event.reason

          // 检查是否应该拦截
          if (
            reason &&
            ((reason.message && reason.message.includes('content_scripts')) ||
              (reason.message && reason.message.includes('trim')) ||
              (reason.message && reason.message.includes('Cannot read properties of null')) ||
              (reason.stack && reason.stack.includes('content_scripts')))
          ) {
            console.warn('🔒 Early interception: content_scripts error blocked:', reason)
            event.preventDefault()
            return false
          }
        })

        // 立即拦截全局错误
        window.addEventListener('error', function (event) {
          if (event.filename && event.filename.includes('content_scripts')) {
            console.warn('🔒 Early interception: content_scripts error blocked:', event.error)
            event.preventDefault()
            return false
          }
        })

        // 保护 String.prototype.trim
        const originalTrim = String.prototype.trim
        String.prototype.trim = function () {
          if (this == null) {
            console.warn('🔒 Early trim protection: null/undefined value intercepted')
            return ''
          }
          if (typeof this !== 'string') {
            console.warn('🔒 Early trim protection: non-string value converted')
            return String(this).trim()
          }
          return originalTrim.call(this)
        }

        // 重写 Promise 构造函数来拦截错误
        const OriginalPromise = window.Promise
        window.Promise = function (executor) {
          return new OriginalPromise((resolve, reject) => {
            try {
              executor(
                (value) => resolve(value),
                (reason) => {
                  // 检查是否应该拦截这个错误
                  if (shouldInterceptEarly(reason)) {
                    console.warn('🔒 Early Promise interception: error blocked:', reason)
                    resolve(null) // 静默处理
                  } else {
                    reject(reason)
                  }
                },
              )
            } catch (error) {
              if (shouldInterceptEarly(error)) {
                console.warn('🔒 Early Promise executor error intercepted:', error)
                resolve(null)
              } else {
                reject(error)
              }
            }
          })
        }

        // 保持 Promise 的静态方法
        Object.setPrototypeOf(window.Promise, OriginalPromise)
        window.Promise.resolve = OriginalPromise.resolve
        window.Promise.reject = OriginalPromise.reject
        window.Promise.all = OriginalPromise.all
        window.Promise.race = OriginalPromise.race
        window.Promise.allSettled = OriginalPromise.allSettled
        window.Promise.any = OriginalPromise.any
        window.Promise.prototype = OriginalPromise.prototype

        // 判断是否应该早期拦截
        function shouldInterceptEarly(error) {
          if (!error) return false

          const errorString = error.toString ? error.toString() : String(error)
          const errorMessage = error.message || errorString
          const errorStack = error.stack || errorString

          return (
            errorString.includes('content_scripts') ||
            errorMessage.includes('content_scripts') ||
            errorStack.includes('content_scripts') ||
            errorString.includes('trim') ||
            errorMessage.includes('trim') ||
            errorStack.includes('trim') ||
            errorString.includes('Cannot read properties of null') ||
            errorMessage.includes('Cannot read properties of null') ||
            errorStack.includes('Cannot read properties of null')
          )
        }

        // 添加额外的错误监控
        const originalConsoleError = console.error
        console.error = function (...args) {
          const errorString = args.join(' ')
          if (errorString.includes('content_scripts') || errorString.includes('trim')) {
            console.warn('🔒 Early console.error interception:', args)
            return
          }
          return originalConsoleError.apply(console, args)
        }

        console.log('✅ Early error interceptor setup completed')
      })()

      // 在 DOM 加载完成后立即设置更强的错误拦截
      document.addEventListener('DOMContentLoaded', function () {
        console.log('🚀 DOM loaded, setting up enhanced error interception...')

        // 再次拦截 unhandledrejection，确保覆盖所有情况
        window.addEventListener('unhandledrejection', function (event) {
          const reason = event.reason

          // 更全面的错误检查
          if (
            reason &&
            ((reason.message &&
              (reason.message.includes('content_scripts') ||
                reason.message.includes('trim') ||
                reason.message.includes('Cannot read properties of null') ||
                reason.message.includes('TypeError'))) ||
              (reason.stack && reason.stack.includes('content_scripts')) ||
              (reason.toString && reason.toString().includes('content_scripts')))
          ) {
            console.warn('🔒 DOM-level interception: Promise rejection blocked:', reason)
            event.preventDefault()
            return false
          }
        })

        // 拦截所有可能的错误
        window.addEventListener('error', function (event) {
          if (event.filename && event.filename.includes('content_scripts')) {
            console.warn('🔒 DOM-level interception: content_scripts error blocked:', event.error)
            event.preventDefault()
            return false
          }

          // 拦截 trim 相关错误
          if (event.message && event.message.includes('trim')) {
            console.warn('🔒 DOM-level interception: trim error blocked:', event.error)
            event.preventDefault()
            return false
          }
        })

        console.log('✅ Enhanced error interception setup completed')

        // 使用 MutationObserver 监控 DOM 变化，拦截新添加的 content_scripts
        const observer = new MutationObserver(function (mutations) {
          mutations.forEach(function (mutation) {
            if (mutation.type === 'childList') {
              mutation.addedNodes.forEach(function (node) {
                if (node.nodeType === Node.ELEMENT_NODE) {
                  // 检查新添加的脚本标签
                  if (node.tagName === 'SCRIPT') {
                    const src = node.src || ''
                    if (src.includes('content_scripts')) {
                      console.warn(
                        '🔒 DOM observer: content_scripts script detected, attempting to block',
                      )
                      // 尝试阻止脚本执行
                      node.remove()
                    }
                  }

                  // 检查新添加的 iframe
                  if (node.tagName === 'IFRAME') {
                    const src = node.src || ''
                    if (src.includes('content_scripts')) {
                      console.warn(
                        '🔒 DOM observer: content_scripts iframe detected, attempting to block',
                      )
                      node.remove()
                    }
                  }
                }
              })
            }
          })
        })

        // 开始监控
        observer.observe(document.documentElement, {
          childList: true,
          subtree: true,
        })

        console.log('✅ DOM mutation observer started')
      })
    </script>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>
